<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Storage, Image;
use Symfony\Component\Finder\Finder;
use App\Lib\XmlToHtml as XmlToHtmlLib;


class XmlToHtml extends Command
{
	/**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'xml:html {id?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'xml to html';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
    	$id = 1;
    	$disk = Storage::disk(config('admin.upload.disk'));
    	$fileName = 'xml.zip'; // get it from DB

    	$rPath = config('admin.upload.directory.xml_html') . '/' . $id;
    	$fileDir = $disk->path($rPath);
        $filePath = $fileDir . '/' . $fileName; // zipfile path

        // before process
        // create html folder
        $newImgDir = $rPath . '/' . 'html/online';
        $disk->makeDirectory($newImgDir); 
        // unzip zip file
    	$dest = $this->unzip($filePath);
        // process image
    	// $this->processImages($dest, $disk->path($newImgDir));

    	$xmlPath = $this->getXmlPath($dest);

        $convert = new XmlToHtmlLib($xmlPath, $fileDir . 'html');
    	
    }



    private function getXmlPath($dest)
    {

    	$finder = Finder::create()->files()->name('*.xml')->in($dest);
    	$count = $finder->count();
    	if ($count !== 1) {
    		throw new \Exception("xml file has problem", 1);
    	}
    	foreach ($finder as $item) {
    		return $item->getRealpath();
    	}
    }

    private function processImages($dest, $newDir)
    {
    	$finder = Finder::create()->directories()->in($dest);
    	$count = $finder->count();
    	if ($count !== 1) {
    		throw new \Exception("too much directories included", 1);
    	}

    	$imageDir = '';
    	foreach ($finder as $item) {
    		$dir = $item->getRealpath();
    		break;
    	}

    	$finderImg = Finder::create()->files()->in($dir);

    	foreach ($finderImg as $item) {
    		$file = $item->getRealpath();
            if (strtolower($item->getExtension()) == 'png') {
                $newFilePath = $newDir . '/' . $item->getFileName();
                Image::make($file)->save($newFilePath);
            } else {
                $newFilePath = $newDir . '/' . preg_replace('/' . $item->getExtension() . '$/i', 'png', $item->getFileName());
                Image::make($file)->encode('png')->save($newFilePath);
            }
            $this->info('conver file: ' . $file);
    	}
    	// print_r($finder->getRealpath);exit;

    }

    private function unzip($source, $destination=null) {
    	if (!$destination) {
            $fileInfo = pathinfo($source);
            $destination = $fileInfo['dirname'] . '/' . $fileInfo['filename'];
        }
    	if (!file_exists($destination)) {
        	@mkdir($destination, 0777, true);
    	}

    	$zip = new \ZipArchive;
        if ($zip->open($source) === true) {
            $zip->extractTo($destination);
            $zip->close();
            return $destination;
        }

        throw new \Exception("can not unzip file", 1);
        

        return false;
    }
}